#version 330 core
out vec4 FragColor;

in vec3 ourColor;
in vec2 TexCoord;

uniform float mixValue;

// texture samplers
uniform sampler2D texture1;
uniform sampler2D texture2;

//void main()
//{
//	// linearly interpolate between both textures
//	FragColor = mix(texture(texture1, TexCoord), texture(texture2, TexCoord), mixValue);
//}

void main()
{
    //首先原始采样像素的 RGBA 值
    // vec4 textureColor =texture2D(uTexture, vTextureCoord);
	vec4 textureColor = texture(texture1, TexCoord);
    //解析点1
    float blueColor = textureColor.b * 63.0;

    //取与 B 分量值最接近的 2 个小方格的坐标
    vec2 quad1; 
    quad1.y = floor(floor(blueColor) / 8.0);   //floor 向下取整 
    quad1.x = floor(blueColor) - (quad1.y * 8.0);
    vec2 quad2;
    quad2.y = floor(ceil(blueColor) / 7.9999);
    quad2.x = ceil(blueColor) - (quad2.y * 8.0);
    
    //解析点2
    vec2 texPos1; 
    texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
    texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);
    vec2 texPos2;
    texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
    texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);

    //取目标映射对应的像素值
    vec4 newColor1 = texture2D(texture2, vec2(texPos1.x,1.0-texPos1.y));
    vec4 newColor2 = texture2D(texture2, vec2(texPos2.x,1.0-texPos2.y));
    
    //解析点3
    vec4 newColor = mix(newColor1, newColor2, fract(blueColor)); 
    
    //解析点4
    gl_FragColor = newColor;

}
